package com.lin.manager.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.manager.common.req.QueryPage;
import com.lin.manager.common.utils.PageUtil;
import com.lin.manager.system.dao.SysUserDao;
import com.lin.manager.system.entity.SysUserEntity;
import com.lin.manager.system.entity.SysUserRoleEntity;
import com.lin.manager.system.service.SysUserRoleService;
import com.lin.manager.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @作者: 林江
 * @创建时间: 2024-03-13 11:48:25
 * @功能:
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageUtil listPage(QueryPage page) {
        LambdaQueryWrapper<SysUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysUserEntity::getCreateTime);
        String key = page.getKey();
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(i -> i.like(SysUserEntity::getName, key).or().like(SysUserEntity::getUsername, key));
        }
        if (page.getDeptId() != null) {
            wrapper.eq(SysUserEntity::getDeptId, page.getDeptId());
        }
        Page<SysUserEntity> selectPage = sysUserDao.selectPage(new Page<>(page.getPageNum(), page.getPageSize()), wrapper);
        return new PageUtil(selectPage);
    }

    @Override
    public void add(SysUserEntity sysUser) {
        String password = sysUser.getPassword();
        if (StringUtils.isEmpty(password)) {
            password = "123456";
        }
        String encode = new BCryptPasswordEncoder().encode(password);
        sysUser.setPassword(encode);
        sysUserDao.insert(sysUser);
    }

    @Override
    public void update(SysUserEntity sysUser) {
        sysUserDao.updateById(sysUser);
    }

    @Override
    public void del(String id) {
        sysUserDao.deleteById(id);
    }

    @Override
    public SysUserEntity getById(Long id) {
        return sysUserDao.selectById(id);
    }

    @Override
    public List<Integer> relative(Long userId) {
        List<SysUserRoleEntity> userRoles = sysUserRoleService.findByUserId(userId);
        return userRoles.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());
    }

    @Override
    public void binding(Long userId, List<Integer> roleIds) {
        // 删除角色
        sysUserRoleService.deleteByUserId(userId);

        List<SysUserRoleEntity> userRoles = roleIds.stream().map(roleId -> {
            SysUserRoleEntity userRole = new SysUserRoleEntity();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            return userRole;
        }).collect(Collectors.toList());

        sysUserRoleService.binding(userRoles);
    }

    @Override
    public SysUserEntity getByUsername(String username) {
        return sysUserDao.selectOne(new LambdaQueryWrapper<SysUserEntity>().eq(
                SysUserEntity::getUsername, username
        ));
    }

    @Override
    public List<SysUserEntity> getByIds(List<Long> ids) {
        return sysUserDao.selectList(new LambdaQueryWrapper<SysUserEntity>().eq(
                SysUserEntity::getId, ids
        ));
    }
}
